package com.book.single.single

import android.app.admin.DevicePolicyManager
import android.content.ComponentName
import android.content.Intent
import android.os.Build
import android.util.Log
import android.view.View
import com.proembed.service.MyService
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant


class MainActivity: FlutterActivity() {

    private var dpm: DevicePolicyManager? = null
    private var inKioskMode = false
    private var deviceAdmin: ComponentName? = null
    private val TAG = MainActivity::class.java.name

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
//        super.configureFlutterEngine(flutterEngine)
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        val android = MethodChannel(flutterEngine.dartExecutor.binaryMessenger,"toAndroid")

        android.setMethodCallHandler { call, result ->
            run {
                if (call.method != null) {
                    if ("hideBar" == call.method) {
                        result.success(hideBar())
                    }
                    if ("showBar" == call.method) {
                        result.success(showBar())
                    }

                } else {
                    result.notImplemented()
                }
            }
        }

    }

    private fun hideBar():String{
        val myService = MyService(this)
        myService.hideNavBar = true
        myService.setSwitch("StatusBar",false)
//        lockScreen()
        return "success"
    }

    private fun showBar():String{
        val myService = MyService(this)
        myService.hideNavBar = false
        myService.setSwitch("StatusBar",true)
        return "success"
    }



    private fun doLockScreen(): Boolean {
        if (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                dpm!!.isLockTaskPermitted(this.packageName)
            } else {
                TODO("VERSION.SDK_INT < LOLLIPOP")
            }
        ) {
            Log.i("yunji.HotelAPP", "start lock screen")
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                startLockTask()
            }
            inKioskMode = true
            Log.i("yunji.HotelAPP", "lock screen success")
            return true
        }
        Log.w("yunji.HotelAPP", "cannot lock screen")
        return false
    }


    private fun lockScreen() {
        try {
            if (!inKioskMode) {
                dpm = getSystemService(DEVICE_POLICY_SERVICE) as DevicePolicyManager

                deviceAdmin = ComponentName(this, AdminReceiver::class.java)

                if(!dpm!!.isAdminActive(deviceAdmin!!)){
                    val intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
                    intent.putExtra(
                        DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                        deviceAdmin
                    )
                    intent.putExtra(
                        DevicePolicyManager.EXTRA_ADD_EXPLANATION,    "设备管理器"
                    )
                    startActivity(intent)
                }

                Log.e(
                    TAG,
                    "isAdminActive: " + dpm!!.isAdminActive(deviceAdmin!!) + "\tisDeviceOwnerApp: " + dpm!!.isDeviceOwnerApp(
                        packageName
                    )
                )
                if (dpm!!.isDeviceOwnerApp(packageName)) {
                    //如果这里失效，请使用adb shell命令设置deviceOwnerAPP为当前app $ adb shell dpm set-device-owner com.yun.yun/.AdminReceiver
                    //参考 https://juejin.im/entry/578f873dd342d30058e99c51
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        dpm!!.setLockTaskPackages(deviceAdmin!!, arrayOf(packageName))
                    }
                    Log.e(TAG, "setLockTaskPackages: ")
                }
                doLockScreen()
            }
        } catch (e: Exception) {
            Log.e("yunji.HotelAPP", "Exception: $e")
        }
    }
}

